#!/usr/bin/env python
# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui
from Ui_dialog_new_relation_type import *
#from EA import *

class dialog_new_relation_type(QtGui.QDialog, Ui_dialog_new_relation_type):
    def __init__(self,parent,ontology):
        super(dialog_new_relation_type,self).__init__(parent)
        self.setupUi(self)
        self.ontology = ontology     
        self.rel_type_id = None #new_rel_type = none  or ew_rel_type = the ID of the new relation type, if it is created
        self.connect(self.buttonBox, QtCore.SIGNAL("accepted()"),self, QtCore.SLOT("accept()"))
        self.connect(self.buttonBox, QtCore.SIGNAL("rejected()"),self, QtCore.SLOT("reject()"))
    
    def accept(self):
        self.name = unicode(self.lineEdit_name.text()).encode('utf-8')
        definition = unicode(self.textEdit_Definition.document().toPlainText()).encode('utf-8')  
        example = unicode(self.lineEdit_example.document().toPlainText()).encode('utf-8')  
        
        if len(self.name)>0 and len(definition)>0 and len(example)>0:
            for k in self.ontology.all_relations_type:
                if self.name == self.ontology.all_relations_type[k].name :
                    QtGui.QMessageBox.warning(self,"myCF Error","A relation type with the name \'%s\' already exists.\nPlease choose an other name."%self.name)
                    return      
            #Execute the SQL query to create the relation type, and retrieve the relation type id in self.rel_type_id
            newRel = self.ontology.add_relation_type(self.name,definition,example) 
            self.rel_type_id = newRel.getId()
            QtGui.QDialog.accept(self)
        else :
            QtGui.QMessageBox.information(self,"myCF information","You must fill all the fields")
